# Contributor: Jeongbin Park <pjb7687@gmail.com>

_realname=eccodes
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.40.0
pkgrel=2
pkgdesc="ECMWF's GRIB and BUFR encoding/decoding library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://confluence.ecmwf.int/display/ECC/ecCodes+Home"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         #"${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://confluence.ecmwf.int/download/attachments/45757960/${_realname}-${pkgver}-Source.tar.gz
        001-fix-mingw.patch
        002-grib-fortran.patch)
noextract=(${_realname}-${pkgver}-Source.tar.gz)
sha256sums=('f58d5d7390fce86c62b26d76b9bc3c4d7d9a6cf2e5f8145d1d598089195e51ff'
            '886d80c00aa760ec3520d7682719a4d8f64dc0d7cd7d0fa5c176beb3ef2b758b'
            '4d73595db12920bbcedb3a39dfad9c96609ac1f196da48935ca543f838a3aaef')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  echo "Extracting ${_realname}-${pkgver}-Source.tar.gz ..."
  tar xf "${srcdir}"/${_realname}-${pkgver}-Source.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}-Source

  apply_patch_with_msg \
    001-fix-mingw.patch \
    002-grib-fortran.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=("-DENABLE_FORTRAN=OFF")
  else
    _extra_config+=("-DENABLE_FORTRAN=ON")
 fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_DATADIR="${MINGW_PREFIX}/share" \
      -DCMAKE_INSTALL_DATAROOTDIR="${MINGW_PREFIX}/share/${_realname}/definitions" \
      "${_extra_config[@]}" \
      -DENABLE_AEC=ON \
      -DENABLE_JPG=ON \
      -DENABLE_JPG_LIBJASPER=ON \
      -DENABLE_JPG_LIBOPENJPEG=ON \
      -DENABLE_NETCDF=ON \
      -DENABLE_PNG=ON \
      -DIEEE_LE=1 \
      -DENABLE_MEMFS=OFF \
      -DENABLE_EXAMPLES=OFF \
      -DENABLE_TESTS=OFF \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../${_realname}-${pkgver}-Source

  "${MINGW_PREFIX}"/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake --install .

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${_PREFIX_WIN}|\$\{prefix\}|g" -i "${pcfile}"
    sed -e "s|.dll||g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}-Source/NOTICE \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/NOTICE
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}-Source/LICENSE \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
